﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Unit : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public int DecimalDigits { get; set; }
        public double Interval { get; set; }
        public decimal MaximumValue { get; set; }
        public decimal MinimumValue { get; set; }
        public string Name { get; set; }
        public List<UnitAbbreviation> Abbrevations { get; set; }

        public Unit()
        {
            this.Abbrevations = new List<UnitAbbreviation>();
        }

        public Unit(XmlNode unitXmlNode):this()
        {
            try
            {
                this.DecimalDigits = int.Parse(XmlHelper.GetAttribute(unitXmlNode, "DecimalDigits"));
                this.Interval = double.Parse(XmlHelper.GetAttribute(unitXmlNode, "Interval"));
                this.MaximumValue = decimal.Parse(XmlHelper.GetAttribute(unitXmlNode, "MaximumValue"));
                this.MinimumValue = decimal.Parse(XmlHelper.GetAttribute(unitXmlNode, "MinimumValue"));
                this.Name = XmlHelper.GetAttribute(unitXmlNode, "Name");

                foreach (XmlNode childNode in unitXmlNode.SelectNodes("UnitAbbreviation"))
                {
                    if (childNode.NodeType == XmlNodeType.Comment)
                        return;

                    UnitAbbreviation ua = new UnitAbbreviation(childNode);
                    this.Abbrevations.Add(ua);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode unitXmlNode = XmlHelper.AddNode(commandUINode, "Unit");
                XmlHelper.AddAttribute(unitXmlNode, "DecimalDigits", this.DecimalDigits);
                XmlHelper.AddAttribute(unitXmlNode, "Interval", this.Interval);
                XmlHelper.AddAttribute(unitXmlNode, "MaximumValue", this.MaximumValue);
                XmlHelper.AddAttribute(unitXmlNode, "MinimumValue", this.MinimumValue);
                XmlHelper.AddAttribute(unitXmlNode, "Name", this.Name);

                foreach (UnitAbbreviation ua in this.Abbrevations)
                {
                    ua.AddNode(unitXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Unit");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "UnitAbbreviation" }, true);

                if (this.Abbrevations.Count == 1)
                {
                    node.ContextMenuStrip.Items[1].Enabled = false;
                }

                foreach (UnitAbbreviation ua in this.Abbrevations)
                {
                    ua.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is UnitAbbreviation))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.Abbrevations == null)
                    {
                        this.Abbrevations = new List<UnitAbbreviation>();
                    }

                    this.Abbrevations.Add((UnitAbbreviation)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is UnitAbbreviation)
                {
                    this.Abbrevations.Remove((UnitAbbreviation)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
